package com.kongwg.preparedStatement1.crud;

import com.kongwg.util.JDBCUtil;
import com.kongwg.util.JDBCUtil1;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * @author kongwg
 * @create 2021-01-07 22:41
 */
public class UpdateTest {
    public void update(String sql, Object... args)  {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JDBCUtil1.getconnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, ps);
        }


    }

    @Test
    public void test1(){
        String sql = "update customers set name = ? where id = ?";
        update(sql,"张飞","1");


        String sql1 = "insert into customers(name, email, birth) values(?,?,?);";
        update(sql1,"kong", "kong@gmail", "2020-02-02");
    }
}
